//
//  XJSCTopicsFormat.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/26.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSCTopicsFormat.h"
#import "XJShouCangTopicsModel.h"
@interface XJSCTopicsFormat()
/**备份数组: 保存网络请求成功后的数据。方便选中取消选中*/
@property (nonatomic, strong) NSMutableArray *backupArr;

@end

@implementation XJSCTopicsFormat
- (NSMutableArray *)backupArr {
    if (!_backupArr) {
        _backupArr = [NSMutableArray array];
    }
    return _backupArr;
}

- (void)requestSCTopicsData {
    
    [XJHttpTool requestInvitationCollectionListSuccessH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            NSArray *list = responseObjc[@"data"][@"list"];
            
            NSMutableArray *tempArr = [NSMutableArray array];
            
            for (NSDictionary *dic in list) {
                XJShouCangTopicsModel * model = [XJShouCangTopicsModel mj_objectWithKeyValues:dic];
                
                [tempArr addObject:model];
            }
            
            self.backupArr = tempArr; //备份:保存一下请求到的数据
            
            [self.delegate requestSCTopicsListSuccessWithArray:tempArr];
            
            [self.delegate xjscTopicsFormatReloadWhenNeed]; //需要刷新的时候的回调。
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark - 二 接口
- (void)selectTopicAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSeleted {
    
    //逻辑:只需要根据indexPath改对应的model的选中状态
    XJShouCangTopicsModel *model = self.backupArr[indexPath.row];
    model.checked = isSeleted;
}

- (void)selectAllTopicWithStatus:(BOOL)isSelected {
    for (XJShouCangTopicsModel *model  in self.backupArr) {
        model.checked = isSelected;
    }
    
    //刷新页面显示效果
    [self.delegate xjscTopicsFormatReloadWhenNeed];
}

- (void)beginToDeleteSeletedTopics {
    NSMutableArray *selectedArr = [NSMutableArray array];
    
    for (XJShouCangTopicsModel *model  in self.backupArr) {
        if (model.checked) {
            [selectedArr addObject:model];
        }
    }
    
    XJLog(@"将要删除的个数:%ld",selectedArr.count);
    [self.delegate xjscTopicsFormatWillDeleteSelectedTopics:selectedArr];
}

/**在 xjscTopicsFormatWillDeleteSelectedTopics 代理方法中调用*/
- (void)deleteTopics:(NSArray *)selectedArr {
    
    if (selectedArr.count == 0) return;
    
    //调删除接口。请求成功刷新页面
    NSString *parmeter = [self assembleRequestParameterWithArr:selectedArr];
    
    [XJHttpTool requestInvitationCollectionDeleteWithIds:parmeter successH:^(id responseObjc) {
    
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            [self requestSCTopicsData]; //重新刷新界面
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark - 三 Private method

//组装请求参数
- (NSString *)assembleRequestParameterWithArr:(NSArray *)arr {
    
    NSMutableString *tempStr = @"".mutableCopy;
    
    for (XJShouCangTopicsModel *model in arr) {
        NSString *topicsId = [NSString stringWithFormat:@"%@,",model.topicsId];
        
        [tempStr appendFormat:@"%@",topicsId];
    }
    
    return [tempStr copy];
}

@end
